package com.yogee.topseller.activity.my;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPicker;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.bean.ContractAddressBean;
import com.yogee.topseller.bean.NewAddressBean;
import com.yogee.topseller.bean.UpdateAddressBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class AddAddressActivity extends HttpActivity {

    @BindView(R.id.add_address_title_layout)
    TitleLayout addAddressTitleLayout;
    @BindView(R.id.address_name_tv)
    TextView addressNameTv;
    @BindView(R.id.address_name_rl)
    RelativeLayout addressNameRl;
    @BindView(R.id.address_name_et)
    ClearEditText addressNameEt;
    @BindView(R.id.address_phone_tv)
    TextView addressPhoneTv;
    @BindView(R.id.address_phone_rl)
    RelativeLayout addressPhoneRl;
    @BindView(R.id.address_phone_et)
    ClearEditText addressPhoneEt;
    @BindView(R.id.address_city_tv)
    TextView addressCityTv;
    @BindView(R.id.address_city_rl)
    RelativeLayout addressCityRl;
    @BindView(R.id.address_city_et)
    TextView addressCityEt;
    @BindView(R.id.address_location)
    RelativeLayout addressLocation;
    @BindView(R.id.address_detailed_tv)
    TextView addressDetailedTv;
    @BindView(R.id.address_detailed_rl)
    RelativeLayout addressDetailedRl;
    @BindView(R.id.address_detailed_et)
    ClearEditText addressDetailedEt;
    @BindView(R.id.address_post_tv)
    TextView addressPostTv;
    @BindView(R.id.address_post_rl)
    RelativeLayout addressPostRl;
    @BindView(R.id.address_post_et)
    ClearEditText addressPostEt;
    @BindView(R.id.address_default_iv)
    ImageView addressDefaultIv;
    @BindView(R.id.address_save_tv)
    TextView addressSaveTv;
    @BindView(R.id.layout_title_right_tv)
    TextView layout_title_right_tv;
    boolean isDefault = false;
    int defaultAddress = 0;
    private String cityArea;
    private String edit;
    private ContractAddressBean.AddressListBean been;
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initView() {
        addAddressTitleLayout.setActivity(this);
        edit = getIntent().getStringExtra("edit");
        addAddressTitleLayout.setRightTV("确定");
        if (edit != null) {
            addAddressTitleLayout.setTitle("修改地址");
            been = (ContractAddressBean.AddressListBean) getIntent().getSerializableExtra("list");
            addressNameEt.setText(been.getName());
            addressPhoneEt.setText(been.getPhone());
            addressCityEt.setText(been.getArea());
            addressDetailedEt.setText(been.getAddress());
            addressPostEt.setText(been.getZipCode());
            cityArea = been.getArea();
            id = been.getId();
            if ("1".equals(been.getDefaultAddress())) {
                addressDefaultIv.setImageResource(R.drawable.select1);
                defaultAddress = 1;
                isDefault = true;
            } else {
                addressDefaultIv.setImageResource(R.drawable.unselect1);
                defaultAddress = 0;
                isDefault = false;
            }
        } else {
            addAddressTitleLayout.setTitle("新增地址");
        }
    }

    @OnClick({R.id.address_location, R.id.address_default_iv, R.id.address_save_tv,R.id.layout_title_right_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_title_right_tv:
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                if (addressNameEt.getText().toString().isEmpty()) {
                    ToastUtils.showToast(this, "请输入收件人姓名");
                } else if (TextUtils.isEmpty(addressPhoneEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入收件人手机");
                } else if (!AppUtil.validatePhone(addressPhoneEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入正确的手机号");
                } else if ("选择所在地区".equals(addressCityEt.getText().toString())) {
                    ToastUtils.showToast(this, "请选择所在地区");
                } else if (TextUtils.isEmpty(addressDetailedEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入详细地址");
                } else if (TextUtils.isEmpty(addressPostEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入邮编");
                } else {
                    if (edit != null) {
                        updateAddress(id,AppUtil.getUserId(this), addressNameEt.getText().toString(), addressPhoneEt.getText().toString(),
                                addressCityEt.getText().toString(), addressDetailedEt.getText().toString(),
                                addressPostEt.getText().toString(), String.valueOf(defaultAddress));
                    } else {
                        newAddress(AppUtil.getUserId(this), addressNameEt.getText().toString(), addressPhoneEt.getText().toString(),
                                addressCityEt.getText().toString(), addressDetailedEt.getText().toString(),
                                addressPostEt.getText().toString(), String.valueOf(defaultAddress));
                    }
                }
                break;
            case R.id.address_location:
                if (TextUtils.isEmpty(cityArea)) {

                    cityArea = "北京市 北京市";
                }
                CityPicker cityPicker = new CityPicker.Builder(AddAddressActivity.this)
                        .textSize(20)
                        .title("城市")
                        .backgroundPop(0x20000000)
                        .titleBackgroundColor("#fefefe")
                        .titleTextColor("#1f1f1f")
                        .confirTextColor("#1f1f1f")
                        .cancelTextColor("#1f1f1f")
                        .province(cityArea.split(" ")[0])
                        .city(cityArea.split(" ")[1])
                        .textColor(Color.parseColor("#6c6c6c"))
                        .provinceCyclic(true)
                        .cityCyclic(false)
                        .districtCyclic(false)
                        .visibleItemsCount(7)
                        .itemPadding(10)
                        .onlyShowProvinceAndCity(true)
                        .build();
                cityPicker.show();

                cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {
                        //省份
                        String provinceSelect = citySelected[0];
                        //城市
                        String citySelect = citySelected[1];

                        cityArea = provinceSelect + " " + citySelect;
                        addressCityEt.setText(cityArea);
                    }
                });
                break;
            case R.id.address_default_iv:
                isDefault = !isDefault;
                if (isDefault) {
                    addressDefaultIv.setImageResource(R.drawable.select1);
                    defaultAddress = 1;
                } else {
                    addressDefaultIv.setImageResource(R.drawable.unselect1);
                    defaultAddress = 0;
                }
                break;
            case R.id.address_save_tv:
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                if (addressNameEt.getText().toString().isEmpty()) {
                    ToastUtils.showToast(this, "请输入收件人姓名");
                } else if (TextUtils.isEmpty(addressPhoneEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入收件人手机");
                } else if (!AppUtil.validatePhone(addressPhoneEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入正确的手机号");
                } else if ("选择所在地区".equals(addressCityEt.getText().toString())) {
                    ToastUtils.showToast(this, "请选择所在地区");
                } else if (TextUtils.isEmpty(addressDetailedEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入详细地址");
                } else if (TextUtils.isEmpty(addressPostEt.getText().toString())) {
                    ToastUtils.showToast(this, "请输入邮编");
                } else {
                    if (edit != null) {
                        updateAddress(id,AppUtil.getUserId(this), addressNameEt.getText().toString(), addressPhoneEt.getText().toString(),
                                addressCityEt.getText().toString(), addressDetailedEt.getText().toString(),
                                addressPostEt.getText().toString(), String.valueOf(defaultAddress));
                    } else {
                        newAddress(AppUtil.getUserId(this), addressNameEt.getText().toString(), addressPhoneEt.getText().toString(),
                                addressCityEt.getText().toString(), addressDetailedEt.getText().toString(),
                                addressPostEt.getText().toString(), String.valueOf(defaultAddress));
                    }
                }
                break;
        }
    }

    private void newAddress(String userId, String name, String phone, String area, String address, String zipCode, String defaultAddress) {
        HttpManager.getInstance().newAddress(userId, name, phone, area, address, zipCode, defaultAddress)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<NewAddressBean>() {
            @Override
            public void onNext(NewAddressBean o) {
                AddAddressActivity.this.loadingFinished();
                if ("1".equals(o.getResult())) {
                    ToastUtils.showToast(AddAddressActivity.this, "新增地址成功");
                    finish();
                }
            }
        }, this));
    }

    private void updateAddress(String id, String userId, String name, String phone, String area, String address, String zipCode, String defaultAddres) {
        HttpManager.getInstance().updateAddress(id, userId, name, phone, area, address, zipCode, defaultAddres)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<UpdateAddressBean>() {
            @Override
            public void onNext(UpdateAddressBean o) {
                AddAddressActivity.this.loadingFinished();
                if ("1".equals(o.getResult())) {
                    Toast.makeText(AddAddressActivity.this, "修改地址成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        }, this));
    }

}